﻿namespace FitnessTracker.Packet
{
    using System;
    using System.Collections.Generic;

    public class HxMPacketPayload
    {
        private readonly byte[] rowBytes;

        public HxMPacketPayload(byte[] buffer)
        {
            this.rowBytes = buffer;
        }

        public byte BatteryChargeIndicator
        {
            get
            {
                return rowBytes[HxMPacketStructure.BatteryChargeIndicatorOffset];
            }
        }

        public byte HeartRate
        {
            get
            {
                return rowBytes[HxMPacketStructure.HeartRateOffset];
            }
        }

        public byte HeartBeatNumber
        {
            get
            {
                return rowBytes[HxMPacketStructure.HeartBeatNumberOffset];
            }
        }

        public List<ushort> HeartBeatTimeStamps
        {
            get
            {
                var heartBeatTimeStamps = new List<ushort>(HxMPacketValueRanges.NumberOfHeartBeatTimeStampsPerPacket);
                for (var i = 0; i < HxMPacketValueRanges.NumberOfHeartBeatTimeStampsPerPacket; i++)
                {
                    var heartBeatTimeStampBytes = new byte[2];
                    heartBeatTimeStampBytes[0] = this.rowBytes[HxMPacketStructure.HeartBeatTimeStampsOffset + (2 * i)];
                    heartBeatTimeStampBytes[1] = this.rowBytes[HxMPacketStructure.HeartBeatTimeStampsOffset + (2 * i) + 1];
                    var heartBeatTimeStamp = BitConverter.ToUInt16(heartBeatTimeStampBytes, 0);
                    heartBeatTimeStamps.Insert(i, heartBeatTimeStamp);
                }

                return heartBeatTimeStamps;
            }
        }

        public double DistanceInMeters
        {
            get
            {
                var distanceBytes = new byte[2];
                distanceBytes[0] = this.rowBytes[HxMPacketStructure.DistanceOffset];
                distanceBytes[1] = this.rowBytes[HxMPacketStructure.DistanceOffset + 1];
                int distanceIn16ThOfMeter = BitConverter.ToUInt16(distanceBytes, 0);
                return distanceIn16ThOfMeter / (double)16;
            }
        }

        public double InstantaneousSpeedInMetersPerSecond
        {
            get
            {
                var instantaneousSpeedBytes = new byte[2];
                instantaneousSpeedBytes[0] = this.rowBytes[HxMPacketStructure.InstantaneousSpeedOffset];
                instantaneousSpeedBytes[1] = this.rowBytes[HxMPacketStructure.InstantaneousSpeedOffset + 1];
                var instantaneousSpeedInStepsOf1By256MetersPerSecond = BitConverter.ToUInt16(instantaneousSpeedBytes, 0);
                return instantaneousSpeedInStepsOf1By256MetersPerSecond / (double)256;
            }
        }

        public byte Strides
        {
            get
            {
                return this.rowBytes[HxMPacketStructure.StridesOffset];
            }
        }

        public override string ToString()
        {
            return
                string.Format(
                    "Packet Size:{0}" + Environment.NewLine + 
                    "BATTERY:{1}" + Environment.NewLine + 
                    "Heart Rate:{2}" + Environment.NewLine +
                    "Heart Beat Number:{3}" + Environment.NewLine +
                    "Heart Beat Time Stamp 0:  {4}" + Environment.NewLine +
                    "Heart Beat Time Stamp 1:  {5}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 2:  {6}" + Environment.NewLine +
                    "Heart Beat Time Stamp 3:  {7}" + Environment.NewLine +
                    "Heart Beat Time Stamp 4:  {8}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 5:  {9}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 6:  {10}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 7:  {11}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 8:  {12}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 9:  {13}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 10: {14}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 11: {15}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 12: {16}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 13: {17}" + Environment.NewLine + 
                    "Heart Beat Time Stamp 14: {18}" + Environment.NewLine +
                    "Distance: {19}" + Environment.NewLine + 
                    "Strides: {20}",
                    this.rowBytes.Length,
                    this.BatteryChargeIndicator,
                    this.HeartRate,
                    this.HeartBeatNumber,
                    this.HeartBeatTimeStamps[0],
                    this.HeartBeatTimeStamps[1],
                    this.HeartBeatTimeStamps[2],
                    this.HeartBeatTimeStamps[3],
                    this.HeartBeatTimeStamps[4],
                    this.HeartBeatTimeStamps[5],
                    this.HeartBeatTimeStamps[6],
                    this.HeartBeatTimeStamps[7],
                    this.HeartBeatTimeStamps[8],
                    this.HeartBeatTimeStamps[9],
                    this.HeartBeatTimeStamps[10],
                    this.HeartBeatTimeStamps[11],
                    this.HeartBeatTimeStamps[12],
                    this.HeartBeatTimeStamps[13],
                    this.HeartBeatTimeStamps[14],
                    this.DistanceInMeters,
                    this.Strides);
        }
    }
}
